/**
 * 
 */
package model;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * @author LeCaoTri
 * @Email: lecaotri23041993@gmail.com
 * 
 */

public class ConnectionDB {
	private String databaseName;
	private String host;
	private String userName;
	private String password;
	private int port;

	private java.sql.Connection connection;

	public ConnectionDB(String host, int port, String databaseName,
			String userName, String password) {
		this.host = host;
		this.databaseName = databaseName;
		this.userName = userName;
		this.password = password;
		this.port = port;
	}

	public ConnectionDB() {
		this.host = "localhost";
		this.port = 3306;
		this.userName = "root";
		this.password = "3121488688";
		this.databaseName = "mobileshop";
	}

	public boolean connect() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://" + this.host + ":" + this.port + "/"
					+ this.databaseName;
			connection = DriverManager.getConnection(url, userName, password);
		} catch (Exception ex) {
			return false;
		}
		return true;
		
	}

	public void disconnect() {
		try {
			this.connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public ResultSet getResult(String query) {
		try {
			connect();
			return connection.prepareStatement(query).executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

	public boolean executeQuery(String query) {
		try {
			connect();
			connection.prepareStatement(query).execute();
		} catch (Exception ex) {
			return false;
		}
		return true;
	}
	public static void main(String[] args) {
		ConnectionDB com = new ConnectionDB();
		System.out.println(com.connect());
	}

}